package com.whsr.readservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.whsr.readservice.service.PageConfigService;
import com.whsr.readservice.util.TreeUtil;
import com.whsr.readservice.vo.PageConfig;
import com.whsr.readservice.vo.PageConfigGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/server/")
public class PageConfigController {

    @Autowired
    private final PageConfigService pageConfigService;
    Map<String, Object> resultMap = new HashMap<>();

    public PageConfigController(PageConfigService pageConfigService) {
        this.pageConfigService = pageConfigService;
    }

    /**
     * @author chatwin
     * @param configGroup 页面传过来的参数
     * @return 返回插入结构的数据
     */
    @ResponseBody
    @RequestMapping("configGroup/add")
    public Object addConfigGroup(@RequestBody PageConfigGroup configGroup){
        configGroup.setCreateTime(new Date());
        configGroup.setUpdateTime(new Date());

        int result = 0;
        resultMap = new HashMap<>();
        try {
            if(configGroup.getId() != 0){
                result = pageConfigService.updateConfigGroup(configGroup);
            }else{
                result = pageConfigService.addConfigGroup(configGroup);
            }

            if(result != 1){
                resultMap.put("errno", 10001);
                resultMap.put("msg", "INSERT DATA ERR");
            }else{
                resultMap.put("errno", 0);
                resultMap.put("msg", "insert data success");
                resultMap.put("data", configGroup);
            }
        }catch (Exception e) {
            resultMap.put("errno", 10001);
            resultMap.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return resultMap;
    }


    /**
     * @author chatwin
     * @param id 休要删除数据的id
     * @param res request
     * @return 成功或者失败的结果
     */
    @ResponseBody
    @RequestMapping("configGroup/remove/{id}")
    public Object removeConfigGroup(@PathVariable int id, HttpServletResponse res){
        int result = 0;
        resultMap = new HashMap<>();
        try {
            result = pageConfigService.removeConfigGroup(id);
            if(result != 1){
                resultMap.put("errno", 10001);
                resultMap.put("msg", "INSERT DATA ERR");
            }else{
                resultMap.put("errno", 0);
                resultMap.put("msg", "insert data success");
            }
        }catch (Exception e){
            resultMap.put("errno", 10001);
            resultMap.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return resultMap;
    }


    /**
     * @author chatwin
     * @param reqMap 页面传过来的参数
     * @return 查询的list列表
     */
    @ResponseBody
    @RequestMapping("configGroup/list")
    public Object getConfigGroupList(@RequestBody Map<String, Object> reqMap){
        resultMap = new HashMap<>();
        List<PageConfigGroup> list = pageConfigService.queryConfigGroup(reqMap.get("name"));
        JSONArray ja = TreeUtil.voListToTree(list, "id", "parentId", "0", "children");
        System.out.println(ja.toString());
        return ja;
    }


    /**
     * @author chatwin
     * @param id 需要查询数据的id
     * @return 返回查询的数据结果
     */
    @ResponseBody
    @RequestMapping("configGroup/getConfigGroup/{id}")
    public Object getConfigGroupById(@PathVariable int id){
        resultMap = new HashMap<>();
        try{
            PageConfigGroup configGroup = this.pageConfigService.getConfigGroupById(id);
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query success");
            resultMap.put("data", configGroup);
        }catch (Exception e){
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query err");
            resultMap.put("data", null);
        }
        return resultMap;
    }

    /**
     * @author chatwin
     * @param reqMap 页面传过来的参数
     * @return 查询的配置信息列表数据
     */
    @ResponseBody
    @RequestMapping("config/list")
    public Object getConfigList(@RequestBody Map<String, Object> reqMap){
        resultMap = new HashMap<>();
        List list = pageConfigService.getConfigList(reqMap.get("name"));
        JSONArray ja = TreeUtil.voListToTree(list, "id", "parentId", "p0", "children");
        System.out.println(ja.toString());
        return ja;
    }

    /**
     * @author chatwin
     * @param id 页面传过来的id
     * @return 返回根据id查询的结果
     */
    @RequestMapping("config/getConfig/{id}")
    @ResponseBody
    public Object getConfigById(@PathVariable int id){
        resultMap = new HashMap<>();
        try{
            PageConfig configGroup = this.pageConfigService.getConfigById(id);
            resultMap.put("errno", 0);
            resultMap.put("msg", "config query success");
            resultMap.put("data", configGroup);
        }catch (Exception e){
            resultMap.put("errno", 0);
            resultMap.put("msg", "config query err");
            resultMap.put("data", null);
        }
        return resultMap;
    }


    /**
     * @author chatwin
     * @param pageConfig 页面传过来的pageConfig参数
     * @return 返回保存pageConfig状态
     */
    @ResponseBody
    @RequestMapping("config/add")
    public Object addConfig(@RequestBody PageConfig pageConfig){
        pageConfig.setCreateTime(new Date());
        pageConfig.setUpdateTime(new Date());

        int result = 0;
        resultMap = new HashMap<>();
        try {
            if(pageConfig.getId() != 0){
                result = pageConfigService.updateConfig(pageConfig);
            }else{
                result = pageConfigService.addConfig(pageConfig);
            }

            if(result != 1){
                resultMap.put("errno", 10001);
                resultMap.put("msg", "INSERT DATA ERR");
            }else{
                resultMap.put("errno", 0);
                resultMap.put("msg", "insert data success");
                resultMap.put("data", pageConfig);
            }
        }catch (Exception e) {
            resultMap.put("errno", 10001);
            resultMap.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * @author chatwin
     * @param id 页面穿过来的id
     * @return 返回删除的结果状态数据
     */
    @ResponseBody
    @RequestMapping("config/remove/{id}")
    public Object removePageConfig(@PathVariable int id){
        int result = 0;
        resultMap = new HashMap<>();
        try {
            result = pageConfigService.removeConfig(id);
            if(result != 1){
                resultMap.put("errno", 10001);
                resultMap.put("msg", "INSERT DATA ERR");
            }else{
                resultMap.put("errno", 0);
                resultMap.put("msg", "insert data success");
            }
        }catch (Exception e){
            resultMap.put("errno", 10001);
            resultMap.put("msg", e.getMessage());
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * @author chatwin
     * @param parentId 页面传过来的父级id
     * @return 返回根据父级id查询的结果
     */
    @RequestMapping("configGroup/getConfigGroupByParentId/{parentId}")
    @ResponseBody
    public Object getConfigGroupByParentId(@PathVariable int parentId){
        resultMap = new HashMap<>();
        try{
            List<PageConfigGroup> list = this.pageConfigService.getConfigGroupByParentId(parentId);
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query success");
            resultMap.put("data", list);
        }catch (Exception e){
            resultMap.put("errno", 0);
            resultMap.put("msg", "configGroup query err");
            resultMap.put("data", null);
        }
        return resultMap;
    }


}
